/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.BillDao;
import gemini.basic.dao.GmnDao;
import gemini.basic.model.Bill;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class BillDaoImpl extends HibernateDaoSupport implements BillDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public Bill saveOrUpdate(Bill bill, boolean flush) {
		Bill result = null;
		if (flush) {
			result = (Bill) gmnDao.saveOrUpdateAndFlush(bill);
		} else {
			result = (Bill) gmnDao.saveOrUpdate(bill);
		}
		return result;
	}

	@Override
	public List<Bill> getBillByDistributorCode(String id) {
		DetachedCriteria dtCri = DetachedCriteria.forClass(Bill.class);

		dtCri.add(Restrictions.eq("distributorCode", id));

		List<Object> billList = gmnDao.findByCriteria(dtCri);

		List<Bill> result = null;

		if (billList != null && !billList.isEmpty()) {
			result = new ArrayList<Bill>(billList.size());
			Bill bill = null;
			for (Object ob : billList) {
				bill = (Bill) ob;
				result.add(bill);
			}
		}

		return result;
	}

}
